package com.applancer.dataprovider;

import java.util.ArrayList;
import java.util.List;

import com.applancer.model.FeedModel;
import com.google.gson.Gson;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "FeedManager.db";

	// Contacts table name
	private static final String TABLE_NAME = "Feed";

	// Contacts Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_DATA = "data";

	private Gson gson;

	public DatabaseHandler(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		gson = new Gson();

	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
				+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATA + " TEXT" + ")";
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

		// Create tables again
		onCreate(db);
	}

	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */

	// Adding new model
	public Boolean addModel(FeedModel model) {

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();

		values.put(KEY_ID, Integer.parseInt(model.getId()));
		values.put(KEY_DATA, gson.toJson(model));

		if (db.insert(TABLE_NAME, null, values) == -1)
			return false;
		db.close();

		return true;
	}

	// Getting single model
	public FeedModel getModel(int id) {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID, KEY_DATA },
				KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null,
				null, null);

		if (cursor == null || cursor.getCount() == 0)
			return null;
		else
			cursor.moveToFirst();

		FeedModel feed = new FeedModel();
		feed = gson.fromJson(cursor.getString(1), FeedModel.class);

		return feed;
	}

	// Getting All feed
	public List<FeedModel> getAllModel() {
		List<FeedModel> modelList = new ArrayList<FeedModel>();
		try {
			// Select All Query
			String selectQuery = "SELECT  * FROM " + TABLE_NAME;

			SQLiteDatabase db = this.getWritableDatabase();
			Cursor cursor = db.rawQuery(selectQuery, null);

			if (cursor.moveToFirst()) {
				do {
					FeedModel feed = new FeedModel();
					feed = gson.fromJson(cursor.getString(1), FeedModel.class);
					modelList.add(feed);
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {

		}
		return modelList;
	}

	// Updating single feed
	public int updateModel(FeedModel model) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_DATA, gson.toJson(model));

		// updating row
		return db.update(TABLE_NAME, values, KEY_ID + " = ?",
				new String[] { model.getId() });
	}

	// Deleting single feed
	public void deleteModel(FeedModel model) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_NAME, KEY_ID + " = ?", new String[] { model.getId() });
		db.close();
	}

	// Getting feed Count
	public int getModelCount() {
		String countQuery = "SELECT  * FROM " + TABLE_NAME;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		return cursor.getCount();
	}

}
